using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Caching;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;



namespace Oplaco.Web.UI
{
    /// <summary>
    /// Utilities class
    /// </summary>
    public sealed class Util
    {
        private Util()
        {
        }

        /// <summary>
        /// Method to find a specific control within controls, like finding a control within a LoginControl
        /// </summary>
        /// <param name="controlID"></param>
        /// <param name="controls"></param>
        /// <returns></returns>
        public static Control FindControlRecursively(string controlID, ControlCollection controls)
        {
            if (controlID == null || controls == null)
                return null;

            foreach (Control c in controls)
            {
                if (c.ID == controlID)
                    return c;

                if (c.HasControls())
                {
                    Control inner = FindControlRecursively(controlID, c.Controls);
                    if (inner != null)
                        return inner;
                }
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetRandomPasswordUsingGUID(int length)
        {
            // Get the GUID
            string guidResult = System.Guid.NewGuid().ToString();

            // Remove the hyphens
            guidResult = guidResult.Replace("-", string.Empty);

            // Make sure length is valid
            if (length <= 0 || length > guidResult.Length)
                throw new ArgumentException("Length must be between 1 and " + guidResult.Length);

            // Return the first length bytes
            return guidResult.Substring(0, length);
        }
    }
}